package com.jf3q.front.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jf3q.common.domain.EduCourse;
import com.jf3q.common.domain.EduTeacher;
import com.jf3q.common.service.EduCourseService;
import com.jf3q.common.vo.ResultVo;
import com.jf3q.common.service.TeacherService;
import com.jf3q.front.vo.PageVo;
import com.jf3q.common.vo.TeacherDetailsVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/frontTeacher")
@Tag(name = "前台讲师")
public class TeachersController {
    @Autowired
    TeacherService teacherService;
    @Autowired
    EduCourseService courseService;

    @PostMapping("/getPage/{page}/{limit}")
    @Operation(summary = "讲师列表")
    public ResultVo getPage(@PathVariable Integer page, @PathVariable Integer limit){
        LambdaQueryWrapper<EduTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(EduTeacher::getSort);
        Page<EduTeacher> pages = teacherService.page(new Page<>(page, limit),queryWrapper);
        PageVo pageVo = new PageVo();
        pageVo.setPage(pages);
        pageVo.setHasPrevious(pages.hasPrevious());
        pageVo.setHasNext(pages.hasNext());
        return ResultVo.success(pageVo);
    }
    @GetMapping("/teacherDetails/{id}")
    @Operation(summary = "讲师详情")
    public ResultVo teacherDetails(@PathVariable String id){
        EduTeacher teacher = teacherService.getById(id);
        TeacherDetailsVo detailsVo = new TeacherDetailsVo();
        detailsVo.setId(teacher.getId());
        detailsVo.setAvatar(teacher.getAvatar());
        detailsVo.setName(teacher.getName());
        detailsVo.setLevel(teacher.getLevel());
        detailsVo.setIntro(teacher.getIntro());
        detailsVo.setCareer(teacher.getCareer());
        LambdaQueryWrapper<EduCourse> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EduCourse::getTeacherId,teacher.getId());
        detailsVo.setCourses(courseService.list(queryWrapper));
        return ResultVo.success(detailsVo);
    }

    @GetMapping("/saveHotTeacher")
    @Operation(summary = "热门讲师加缓存")
    public ResultVo saveHotTeacher(){
        teacherService.saveTeacherRank();
        return ResultVo.success();
    }
    @GetMapping("/addTeacherVideo")
    @Operation(summary = "热门讲师加缓存+1")
    public ResultVo addTeacherVideo(){
        teacherService.playTeacherVideo("1651428000666666666");
        return ResultVo.success();
    }
}
